package com.tem.system.controller;

import com.api.manage.pojo.system.SysBranchOfficeResources;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tem.system.service.ISysCompanyService;
import com.tem.system.service.ISystemLevelBranchService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.system.entity.SysBranchOffice;
import com.api.manage.pojo.system.SysBranchOfficeBean;
import com.tem.system.service.ISysBranchOfficeService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 分公司 分店 前端控制器
 * </p>
 * @author 腾木
 * @since 2023-11-17
 */
@RestController
@RequestMapping("/system/sysBranchOffice")
public class SysBranchOfficeController extends BaseController{
    @Autowired
    private ISysBranchOfficeService sysBranchOfficeService;
    @Autowired
    private ISystemLevelBranchService systemLevelBranchService;
    @Autowired
    private ISysCompanyService sysCompanyService;
    @ApiOperation(value = "分公司 分店列表")
    @RequestMapping("list")
    public ResultUtil<IPage<SysBranchOfficeBean>> list(SysBranchOfficeBean request, HttpServletRequest req){
        QueryWrapper<SysBranchOffice> wrapper = new QueryWrapper<>();
        if(request.getSysCompanyId()==null){
            request.setSysCompanyId(this.getSysCompanyId(req));
        }
        wrapper.lambda().eq(SysBranchOffice::getSysCompanyId,request.getSysCompanyId());
        wrapper.orderByDesc("sys_branch_office_id");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = sysBranchOfficeService.page(page,wrapper);
        List<SysBranchOffice> entityList=listBody.getRecords();
        String companyName=sysCompanyService.getById(request.getSysCompanyId()).getCompanyName();
        if(!entityList.isEmpty()) {
            List<SysBranchOfficeBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                SysBranchOfficeBean bean = new SysBranchOfficeBean();
                BeanUtils.copyProperties(it,bean);
                bean.setSysCompanyIdName(companyName);
                bean.setSystemLevelBranchList(systemLevelBranchService.getSystemLevelBranchBean(it.getSysBranchOfficeId()));
                responseList.add(bean);
            });
            listBody.setRecords(responseList);
        }
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增分公司 分店")
    public ResultUtil save(@RequestBody SysBranchOffice entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getSysBranchOfficeId()!=null){
            sysBranchOfficeService.updateById(entity);
        }else{
            entity.setDeleted(0);
            sysBranchOfficeService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询分公司 分店")
    public ResultUtil<SysBranchOfficeBean> getById(@PathVariable String id){
        SysBranchOffice entity = sysBranchOfficeService.getById(id);
        if(entity!=null){
            SysBranchOfficeBean bean = new SysBranchOfficeBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改分公司 分店")
    public ResultUtil update(@RequestBody SysBranchOffice entity){
        sysBranchOfficeService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除分公司 分店")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        sysBranchOfficeService.removeById(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取已购买的所有系统")
    @GetMapping("getAllResources/{id}")
    public ResultUtil<SysBranchOfficeResources> getAllResources(@PathVariable Long id){
        SysBranchOfficeResources resources = sysBranchOfficeService.getAllResources(id);
        return ResultUtil.success(resources);
    }
}